package com.dev.dream.libs.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.dev.dream.libs.mylib.R;
import com.dev.dream.libs.utils.BitmapCache;

public class ImageAdapterGridView extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context mContext;
	private ArrayList<ImageBitmap> photos = new ArrayList<ImageBitmap>();
	//private final BitmapCache mMemoryCache;

	public ImageAdapterGridView(Context context, ArrayList<ImageBitmap> imgs) {
		mContext = context;
		photos = imgs;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//mMemoryCache = new BitmapCache();
	}

	public int getCount() {
		return photos.size();
	}

	public Object getItem(int position) {
		return photos.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.gridview_custom, null);
			holder.imageview = (ImageView) convertView
					.findViewById(R.id.img_item);
			
			holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);

			// holder.checkbox = (CheckBox)
			// convertView.findViewById(R.id.cb_item);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		int imageResId = photos.get(position).getId();
		holder.id = position;

		//Bitmap bitmap = getBitmapFromMemCache(imageResId);
//		if (bitmap == null) {
//			bitmap = photos.get(position).getBmp();
//			addBitmapToMemoryCache(imageResId, bitmap);
//			//Log.d("Kai", "Add to bitmap");
//		}else{
//			//Log.d("Kai", "recycle");
//		}
		// imageView.setImageBitmap(bitmap);

		holder.imageview.setImageBitmap( photos.get(position).getBmp());

		return convertView;
	}

//	private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
//		if (getBitmapFromMemCache(key) == null) {
//			mMemoryCache.put(key, bitmap);
//		}
//	}
//
//	private Bitmap getBitmapFromMemCache(final int key) {
//		return mMemoryCache.get(key);
//	}

	class ViewHolder {
		ImageView imageview;
		int id;
	}

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

}

/**
 * A LoadedImage contains the Bitmap loaded for the image.
 */

